// @algorithm @lc id=1000306 lang=cpp
// @title 3Etpl5

#include "algm/algm.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    // @test([1,2,3])=25
public:
    int ans = 0;
    int sumNumbers(TreeNode *root) {
        int temp = 0;
        dfs(root, temp);
        return ans;
    }

    void dfs(TreeNode *root, int &temp) {
        temp = temp * 10 + root->val;
        if (root->left == nullptr && root->right == nullptr) {
            ans += temp;
        }
        if (root->left) {
            dfs(root->left, temp);
        }
        if (root->right) {
            dfs(root->right, temp);
        }
        temp = temp / 10;
        return;
    }
};